package com.xinlong.shop.api.temp;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinlong.shop.core.temp.entity.BuyGroup;
import com.xinlong.shop.core.temp.entity.VipMember;
import com.xinlong.shop.core.temp.service.IBuyGroupService;
import com.xinlong.shop.core.temp.service.IVipMemberService;
import com.xinlong.shop.framework.common.R;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 抢购老系统临时使用 前端控制器
 * </p>
 *
 * @author Sylow
 * @since 2023-04-07
 */
@RestController
@RequestMapping("/temp/buy-group")
public class BuyGroupController {

    private final IBuyGroupService buyGroupService;
    private final IVipMemberService vipMemberService;

    public BuyGroupController(IBuyGroupService buyGroupService, IVipMemberService vipMemberService) {
        this.buyGroupService = buyGroupService;
        this.vipMemberService = vipMemberService;
    }

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public R page(Integer current, Integer size){
        Page<BuyGroup> page = new Page<>(current, size);
        page = this.buyGroupService.page(page);
        return R.success("操作成功", page);
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public R save(@Valid @RequestBody BuyGroup buyGroup ){
        buyGroup.setCreateTime(DateUtil.currentSeconds());
        this.buyGroupService.save(buyGroup);
        return R.success("操作成功");
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public R update(@Valid @RequestBody BuyGroup buyGroup, @PathVariable("id") Integer id){
        this.buyGroupService.update(buyGroup, id);
        return R.success("操作成功",buyGroup);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public R delete(@PathVariable("id") Integer id) {
        this.buyGroupService.delete(id);
        return R.success("删除成功");
    }

    @RequestMapping(value = "/is-vip", method = RequestMethod.GET)
    public R isVip(Integer memberId) {

        VipMember vip = this.vipMemberService.find(memberId);

        if (vip != null) {
            // 0是vip
            return R.success("成功", 0);
        }
        return R.success("失败",1);
    }

    @RequestMapping(value = "/find-one", method = RequestMethod.POST)
    public R findOne(@Valid @RequestBody BuyGroup buyGroup) {

        BuyGroup tempBuy = this.buyGroupService.find(buyGroup.getRuleId(), buyGroup.getGoodsId(), buyGroup.getProductId());

        // 有数据就被占用
        if (tempBuy == null) {

            return R.success("成功",0);
        }

        return R.success("失败",1);
    }

    @RequestMapping(value = "/find-list", method = RequestMethod.GET)
    public R findList(Integer memberId, String ruleId) {

        List<BuyGroup> list = this.buyGroupService.findList(ruleId, memberId);

        return R.success("成功",list);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public R delete(Integer memberId, String ruleId, Integer goodsId) {
        this.buyGroupService.delete(memberId, ruleId, goodsId);
        return R.success("删除成功");
    }

    @RequestMapping(value = "/batch-delete", method = RequestMethod.GET)
    public R batchDelete(Integer memberId) {
        this.buyGroupService.batchDelete(memberId);
        return R.success("成功");
    }

}
